/*
 * Copyright (c) 2012 Robert Burrell Donkin robertburrelldonkin.name
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software 
 * is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 */
package walrus.unpack.xml;

import java.io.File;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;


public abstract class DumpParser extends DumpLoader {

	public enum Section {
		TITLE, TEXT, OTHER
	};

	private Section section = Section.OTHER;

	protected abstract void title(char[] ch, int start, int length);

	protected abstract void text(char[] ch, int start, int length);

	protected abstract void startSection(Section section);

	protected abstract void endText();
	
	public DumpParser(File target) {
		super(target);
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		final Section oldSection = this.section;
		if ("title".equals(qName)) {
			section = Section.TITLE;
		} else if ("text".equals(qName)) {
			section = Section.TEXT;
		} else {
			section = Section.OTHER;
		}
		if (oldSection != section) {
			startSection(section);
		}
	}

	
	
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		super.endElement(uri, localName, qName);
		if ("text".equals(qName)) {
			endText();
		}
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		super.characters(ch, start, length);
		switch (section) {
		case TEXT:
			text(ch, start, length);
			break;
		case TITLE:
			title(ch, start, length);
			break;
		}
	}

}